<script setup>
import {ref, watch} from 'vue'
import {useMouseInElement} from '@vueuse/core'

    // 动态传入imageList图片
    defineProps({
            imageList: {
                type: Array,
                default() {
                    return []
                }
            }
        })
    
// 1. 小图切换大图显示
        const currentIndex = ref(0)
        function LiEnter(index) {
            currentIndex.value = index
        }

// 2. 获取鼠标相对位置
        const target = ref(null)
        const {elementX, elementY, isOutside} = useMouseInElement(target)

// 3. 控制滑块跟随鼠标移动（监听elementX/Y变化，一旦变化，就重置left,top
    const left = ref(0)
    const top = ref(0)
    watch([elementX, elementY, isOutside], () => {
        // console.log('xy变化了');
        // 如果鼠标没有移入中等盒子，就不执行后续逻辑
        if(isOutside.value) return
        // console.log('后续逻辑执行了');
        if (elementX.value > 100 && elementX.value < 300) {
            left.value = elementX.value - 100
        }
        if (elementY.value > 100 && elementY.value < 300) {
            top.value = elementY.value - 100
        }

        // 处理边界
        if (elementX.value < 100) {left.value = 0}
        if (elementX.value > 300) {left.value = 200}

        if (elementY.value < 100) {top.value = 0}
        if (elementY.value > 300 ) {top.value = 200}
    })
</script>


<template>
     <!-- 图片预览区 -->
     <div class="goods-image">
        <div class="middle" ref="target">
            <img :src="imageList[currentIndex]" alt="">
            <div class="layer" :style="{left: `${left}px`, top: `${top}px`}"></div>
        </div>
        <ul class="small">
            <li v-for="item, index in imageList"
                :key="item" @mouseenter="LiEnter(index)"
                :class="{active: index === currentIndex}">
                <img :src="item" alt="">
            </li>
        </ul>
        <div class="big" v-show="!isOutside" :style="[
            {
                backgroundImage: `url(${imageList[currentIndex]})`,
                backgroundPositionX: `${-left * 2}px`,
                backgroundPositionY: `${-top * 2}px`
            }
        ]"></div>
    </div>
</template>


<style scoped lang='scss'>
    .goods-image{
        position: relative;
        display: flex;
        width: 480px;
        height: 400px;
        .middle{
            position: relative;
            width: 400px;
            height: 400px;
            .layer{
                width: 200px;
                height: 200px;
                background: rgba(0, 0, 0, .2);
                position: absolute;
                left: 0;
                top: 0;
            }
        }
        .small{
            width: 80px;
            li{
                width: 68px;
                height: 68px;
                margin-left: 12px;
                margin-bottom: 15px;
                cursor: pointer;
                &:hover,
                &.active{
                    border: 2px solid $xtxColor;
                }
                
            }
        }
        .big{
            position: absolute;
            left: 412px;
            top: 0;
            width: 400px;
            height: 400px;
            background-size: 800px 800px;
            background-repeat: no-repeat;
            z-index: 9;
        }
    }
</style>